#include <gflags/gflags.h>
#include "doca_brpc_unit.h"
#include "dmatest.pb.h"
#include "timer.h"

DEFINE_int32(dma_task_num, 20, "dma_task_num");
DEFINE_int32(dma_task_size, 2000000, "dma_task_size");
DEFINE_int32(brpc_byte, 4, "brpc_task_bytes");

void communication_bench() {
    std::vector<size_t> offsets;
    offsets.reserve(FLAGS_dma_task_num + 1);
    size_t total_size = 0;
    for (int i = 0; i < FLAGS_dma_task_num; ++i) {
        offsets.push_back(total_size);
        total_size += (size_t)(FLAGS_dma_task_size);
    } offsets.push_back(total_size);

    // brpc init
    BrpcUnit * brpcunit = new BrpcUnit();
    brpcunit->InitDmaUnit(offsets);
    brpcunit->InitBrpc();
    brpcunit->SendDmaInfo(0, 0, 1, FLAGS_dma_task_num, offsets);

    //test dma, transfer dma_task_num * dma_task_size;
    std::vector<int> task_mem_list;
    LOG(INFO) << "start dma engine";
    task_mem_list.resize(FLAGS_dma_task_num);
    xmh::Timer dma_timer("dma_task_num_" + std::to_string(FLAGS_dma_task_num)
        + "_task_size_" + std::to_string(FLAGS_dma_task_size) );
    for (int i = 0; i < FLAGS_dma_task_num; ++i) {
        task_mem_list[i] = i;
    }
    brpcunit->task_dispatch_mem(task_mem_list);
    dma_timer.end();
    LOG(INFO) << "end dma engine";

    std::vector<int> brpc_task_lists;
    brpc_task_lists.resize(FLAGS_brpc_byte / 4);
    for (auto & task_op : brpc_task_lists) {
        task_op = 1;
    }

    LOG(INFO) << "start brpc transfer";

    xmh::Timer brpc_timer("brpc_byte_" + std::to_string(FLAGS_brpc_byte));
    brpcunit->task_dispatch_op(brpc_task_lists);
    brpc_timer.end();

    LOG(INFO) << "end brpc transfer";
}

int main(int argc, char **argv) {
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    xmh::Reporter::StartReportThread();
    communication_bench();
    xmh::Reporter::StopReportThread();
    xmh::Reporter::Report();

    return 0;
}